<template>
  <div class="pxy20">
    <ElForm
      ref="articleRef"
      :model="ArticleData"
      label-width="80px"
      label-position="left"
      :rules="articleRules"
    >
      <ElFormItem
        label="所属板块"
        prop="position"
        enctype="multipart/form-data"
      >
        <ElSelect v-model="ArticleData.position" placeholder="">
          <ElOption key="常规位" value="常规位">常规位</ElOption>
          <ElOption key="banner位" value="banner位">banner位</ElOption>
          <ElOption key="推荐位" value="推荐位">推荐位</ElOption>
        </ElSelect>
      </ElFormItem>
      <ElFormItem label="日期" prop="articleDate">
        <el-date-picker
          v-model="ArticleData.articleDate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        />
      </ElFormItem>
      <ElFormItem
        label="标题"
        prop="title"
      ><ElInput
        v-model="ArticleData.title"
        style="width: 270px"
        placeholder="请输入文章标题"
      /></ElFormItem>

      <ElFormItem
        label="概述"
        prop="articleSummary"
      ><ElInput
        v-model="ArticleData.articleSummary"
        style="width: 280px"
        placeholder="请输入文章概述"
      /></ElFormItem>
      <ElFormItem
        label="标签"
        prop="tag"
      ><ElInput
        v-model="ArticleData.tag"
        style="width: 250px"
        placeholder="请输入文章标签"
      /></ElFormItem>
      <ElFormItem label="封面" prop="image">
        <el-upload
          class="avatar-uploader"
          action="https://api.ukshuxi.com/api/file/uploadFile"
          :show-file-list="false"
          :on-success="uploadSuccess"
        >
          <img
            v-if="ArticleData.image"
            :src="baseUrl+ArticleData.image"
            class="avatar"
          >
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </ElFormItem>
      <ElFormItem label="正文" prop="content">
        <editor v-model="ArticleData.content" />
      </ElFormItem>
    </ElForm>
    <div style="margin-left: 80px; margin-bottom: 100px">
      <ElButton type="primary" @click="submitBtnEve">发布</ElButton>
      <ElButton type="info" @click="resetBtnEve">重置</ElButton>
    </div>
  </div>
</template>
<script>
import { articleRules } from '@/utils/formVerify'
import editor from './editor.vue'
import { ADD_ARTICLE, EDIT_ARTICLE } from '@/api/article'
export default {
  name: 'ArticleEdit',
  components: { editor },
  data() {
    return {
      ArticleData: {
        content: '',
        position: '常规位',
        articleDate: '',
        image: '',
        articleSummary: '',
        title: '',
        tag: ''
      },
      articleRules,
      baseUrl: 'https://api.ukshuxi.com'
    }
  },
  created() {},
  mounted() {
    const row = this.$route.params
    Object.keys(row).length === 0 ? '' : (this.ArticleData = row)
    console.log('文章信息', row)
  },
  methods: {
    // 发布
    submitBtnEve() {
      this.$refs['articleRef'].validate((is) => {
        if (is) {
          // 表单验证
          this.ArticleData.id // 根据ID判定是否编辑文章
            ? EDIT_ARTICLE(this.ArticleData).then((res) => {
              console.log('编辑文章', res)
              this.initForm(res)
            })
            : ADD_ARTICLE(this.ArticleData).then((res) => {
              console.log('增加文章', res)
              this.initForm(res)
            })
        }
      })
    },
    // 接口执行反馈
    initForm(res) {
      this.$message({
        message: res.code === '200' ? '文章发布成功！' : '文章发布失败!',
        type: res.code === '200' ? 'success' : 'error'
      })
      res.code === '200' ? this.resetBtnEve() : ''
    }, // 表单初始化
    resetBtnEve() {
      this.$refs['articleRef'].resetFields()
    },
    // 图片上传成功
    uploadSuccess(res, file, fileList) {
      console.log(res)
      this.ArticleData.image =
        '/drdImg/' + res.data.fileName
      this.$message({
        message:
          res.code === 200 ? '封面上传成功!' : '封面上传失败，请联系系统管理员!',
        type: res.code === 200 ? 'success' : 'error'
      })
    },
    // 图片上传前验证
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
